<?php
namespace Products\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class BidTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addbiding($biddetails,$productdetails,$bidprice)
    {
		$user_id_user=explode("/",$biddetails['userdropdown']);
		if(isset($productdetails->suggested_price) && $productdetails->suggested_price!=""){
			$price=$productdetails->suggested_price;
		}else{
			$price="";
		}
		if(isset($productdetails->quntity) && $productdetails->quntity!=""){
			$quantity=$biddetails['userPacks']*$biddetails['hidBuyproductPer'];
		}else{
			$quantity="";
		}
		$data = array(
			'category_id'        => $productdetails->category_id,
			'product_id'         => $productdetails->product_id, 	
			'user_id' 	         => $user_id_user[0], 	
			'business_user_id' 	 => $productdetails->user_id,			
			'biding_quantity' 	 => $quantity,	
			'actual_price' 	     => $price,	
			'biding_price' 	 	 => $bidprice,	
			'biding_date_time' 	 => date('Y-m-d H:i:s'),	
			'bid_status' 	     => 0,	
		);
		$this->tableGateway->insert($data);		
		return $this->tableGateway->lastInsertValue;
    }
	public function bidlist($pid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->where('bid.bid_id="'.$pid.'"');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->current();	 
	}
	public function user_details($pid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->where('bid.bid_id="'.$pid.'"');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->current();	 
	}	
	public function allbids(){
		$select = $this->tableGateway->getSql()->select();	
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
	public function bidingDetails($biddetails) {
		$select = $this->tableGateway->getSql()->select();
		$select->where('product_id="'.$biddetails['bid_product'].'"');		
		$select->where('bid_id="'.$biddetails['bid_id'].'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();	 
	} 
    public function getbidingDetails($bidData) {
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->where('bid.product_id="'.$bidData.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;	 
	}
	 public function getbidinglist($bidData) {
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->where('bid.product_id="'.$bidData.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet->current();	 
	}
	public function getPending($userid){
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_id="'.$userid.'"');
		$select->where('bid_status=0');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();		
	}
	public function existingBidsOnProducts($userid,$categoryid){
	    $select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'productsnew.user_id=users_new.user_id',array('*'),'left');
		$select->where('bid.user_id="'.$userid.'"');
		if($categoryid!='all'){
			$select->where('bid.category_id="'.$categoryid.'"');
		}
		$resultSet = $this->tableGateway->selectWith($select);
		
		return $resultSet;		
	}
	public function statusExstingbids($pro_id) {
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->where('bid.product_id="'.$pro_id.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;	 
	}
	public function statusOnBid($bidDetais){
		if($bidDetais['status']='accepted'){
			$status=1;
		}else if($bidDetais['status']='cancelled'){
			$status=0;
		}
		$data = array(
			'bid_status' 	     => $status,
		);
		$row=$this->tableGateway->update($data, array('bid_id' => $bidDetais['hid_bid_id']));				
		return $row;	
	}
	public function bidDetails($bid_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->where('bid.bid_id="'.$bid_id.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet->current();
	}
	public function bidrecivedCount($userId){
		$select = $this->tableGateway->getSql()->select();
		$select->where('business_user_id="'.$userId.'"');
		$select->where('bid_status="0"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();
	}
	public function changeStatusbidProduct($bid_id,$newStatus){
		$data = array(
			'bid_status' 	     => $newStatus,
		);
		$row=$this->tableGateway->update($data, array('bid_id' =>$bid_id));				
		return $row;	
	}
	public function getDetailesOfProduct($bidId) {
	    $select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'bid.user_id=users_new.user_id',array('*'),'left');
		$select->join('history', 'bid.bid_id=history.id',array('*'),'left');
		$select->where('bid.bid_id="'.$bidId.'"');
		$select->where('history.type="bid"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;	 
	}
	public function existingBidsOnusersProducts($all_userids){
		$userIds='';
		for($i=0;$i<count($all_userids);$i++) {
			$userIds.=$all_userids[$i] . ",";
		}
		$userIds = substr($userIds,0,(strlen($userIds)-1));
	    $select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'bid.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'productsnew.user_id=users_new.user_id',array('*'),'left');
		$select->where(array('(bid.user_id  IN ('.$userIds.'))'));
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;		
	}
}